#!/bin/bash
#功能函数
funcs=`which functions.sh`
if [ "$funcs" != "" ] ;then
    . $funcs
else
    funcs=`dirname $0`/functions.sh
    echo "$PWD"
    echo "funcs=$funcs"
    if [ "$funcs" != "" ] ;then
	. $funcs
    else
	exit
    fi
fi

#####################################
share_dirs="/opt/shared/smb/public"
#需要安装的文件列表
file_list="samba smbfs smbclient"
#nmb 用于解析服务器名称到ip地址
#使用Samba实现linux(Ubuntu)与windows互访 
#http://www.redbots.cn/linux/2008/10/05/1991.htm
#####################################

if [ "$funcs" = "" ] 
then
    sudo apt-get install $file_list
else
    sh_dpkg_install $file_list
fi 

result=`question "是否共享默认目录： $share_dirs" 1 yes`
if [  "$result" = yes ] ; then
  for dir in $share_dirs
    do	
      if test ! -d $dir; then
      result=`question "目录 $dir 不存在,是否创建?" 1 no`
        if [  "$result" = yes ] ; then
          mkdir -pv $dir
          shared_dirs="$dir $shared_dirs"
        fi
      else
        shared_dirs="$dir $shared_dirs"
      fi
  done
fi

echo "$shared_dirs"

result=`question "是否手动指定共享目录？" 1 no`
if [  "$result" = yes ] ; then
  set_share_dir=true
  while $set_share_dir
  do
      ac_dir=`question  "请输入需要共享的目录,结束直接回车："  2 ""`
      echo "ac_dir=$ac_dir"
      if [ "$ac_dir" = "" ]; then
          set_share_dir=false
      else
          shared_dirs="$ac_dir $shared_dirs"
      fi
  done
fi
echo "$shared_dirs"

#if test -f /etc/samba/smb.conf; then
#  result=`question "是否备份默认配置文件 /etc/samba/smb.conf？" 1 no`
#  if [  "$result" = yes ] ; then
#    echo "mv /etc/samba/smb.conf /etc/samba/smb.conf.$ac_time_date"
#    sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.$ac_time_date 
#  fi
#fi


ac_file=~/samba_config
echoColor "＊＊＊＊设置samba服务器" GREEN
ac_files=`ls /var/lib/samba/usershares/* -d 2>/dev/null`
for file in $ac_files
do
    ac_dir=`cat $file |grep path |sed 's/path=//g'`
    if [ -n "$ac_dir" ]; then
        ac_dirs="$ac_dirs $ac_dir"
    fi
done
if [ -n "$ac_dirs" ]; then
    result=`question "是否覆盖默认共享目录 $ac_dirs" 1 no`
    if [ "$result" = "no" ]; then
        shared_dirs="$shared_dirs $ac_dirs"
    fi
fi
for dir in $shared_dirs
do 
  result=`question "是否允许匿名访问 $dir" 1 no`
  if [ "$result" = "yes" ]; then
    ac_guest=y
  else
    ac_guest=n
  fi
ac_file=/var/lib/samba/usershares/`basename $dir`
share_name=`basename $dir`
share_name=`question  "请输入共享名称，默认为:"  2 "$share_name"`
echoTextBlue "net usershare add  $share_name $dir \"info:$share_name\" \"$USER:f\" guest_ok=$ac_guest"
net usershare add  $share_name $dir "info:$share_name" "$USER:f" guest_ok=$ac_guest
#net usershare add share /home/mhf/desktop/share/ "info:share" everyone:r guest_ok=y

done

function smbd_restart()
{
    sudo /etc/init.d/smbd restart
}
function samba_restart()
{
    sudo /etc/init.d/smbd restart
}

##派断是否为 32 位系统
function ubuntu_version_is()
{
    result=`cat /etc/issue |grep $1`
if [ "$result" = "" ]
then
return 0
else
return 1
fi 
}

echoColor "＊＊＊＊重启samba服务器" GREEN
samba_v=`sh_get_ubuntu_version`
if ubuntu_version_is "10.10"; then
    smbd_restart
elif ubuntu_version_is "10.04"; then
    smbd_restart
else
    samba_restart
fi
exit
直接用smbpasswd设置就可以了，不用去管那个密码文件。
现在samba的密码是放在/var/lib/samba中。
清除 windows 保存的网络登录密码
control keymgr.dll

ubuntu samba 权限设置
http://huzhangsheng.blog.163.com/blog/static/3478778420103138102093/

ubuntu 10.10服务器搭建之samba
http://hi.baidu.com/hythyt9898/blog/item/e0666f27124f5c7135a80f4e.html

http://socol.javaeye.com/blog/551219
为使用samba 的net usershare命令，需要创建共享路径"/var/lib/samba/usershares"和组sambashare
并且将已有的用户添加到sambashare中。
Bash代码
   1. #! /bin/sh  
   2. USERSHARES_DIR="/var/lib/samba/usershares"  
   3. USERSHARES_GROUP="sambashare"  
   4. HAVE_SHARES_GROUP=`awk -F : '{name = $1;if(name=="sambashare")print $1}' /etc/group`  
   5. if test "x$HAVE_SHARES_GROUP" = "x" ; then  
   6.         /usr/sbin/groupadd ${USERSHARES_GROUP}  
   7. fi  
   8. mkdir -p ${USERSHARES_DIR}  
   9. chown root:${USERSHARES_GROUP} ${USERSHARES_DIR}  
  10. chmod 01770 ${USERSHARES_DIR}  
  11. for USERSHARES_NAME in $(grep /home /etc/passwd | cut -f1 -d:)  
  12. do  
  13.         /usr/sbin/usermod -a -G ${USERSHARES_GROUP} ${USERSHARES_NAME}  
  14. done  


#nmbd 由 samba 包提供

